﻿//��ѯ
(function(){
var Poll=function()
	{
	this.GetArgument(arguments[0],{Domain:null,Path:null,VerifyPath:'poll.Verify',Query:{},IsView:1,ReturnName:null},1);
	if(this.Path==null)	this.Path=this.IsView?'/poll.html':'poll';
	this.GetFunction=fastCSharp.ThisFunction(this,this.Get);
	this.VerifyFunction=fastCSharp.ThisFunction(this,this.Verify);
	this.OnVerifyFunction=fastCSharp.ThisFunction(this,this.OnVerify);
	this.OnMessage=fastCSharp.Event();
	this.VerifyTimeout=100;
	};
(fastCSharp.Functions.Poll=Poll).Inherit(fastCSharp.BaseFunction,{
Start:function(Verify)
	{
	if(Verify!=null)	this.VerifyInfo=Verify;
	if(!this.Identity)	setTimeout(this.GetFunction,this.Identity=1);
	},
Stop:function()
	{
	this.Identity=0;
	},
Get:function()
	{
	if(this.Identity)
		{
		if(this.OnMessage.Get().length)
			{
			fastCSharp.AppendJs((this.Domain==null?'//127.0.0.1:8112':(this.Domain?'//'+this.Domain:''))+'/ajax?n='+this.Path+'&c='+this.SysObject+'.OnGet'+(this.IsView?'View':'')+'&j='+fastCSharp.ToJson({verify:this.VerifyInfo,query:this.Query},1).Escape()+(fastCSharp.LoadJs.IsRandom?'&t='+(new Date).getTime():''),null,null,fastCSharp.ThisFunction(this,this.OnError,[this.Identity]));
			}
		else	setTimeout(this.GetFunction,1000);
		}
	},
OnGet:function(Value)
	{
	this.OnGetView(Value?Value.Return:null);
	},
OnGetView:function(Value)
	{
	if(this.Identity)
		{
		++this.Identity;
		if(Value)
			{
			if(!this.VerifyPath||Value.isVerify)
				{
				this.VerifyTimeout=100;
				if(this.ReturnName)
					{
					if(Value[this.ReturnName])	this.OnMessage(Value[this.ReturnName]);
					}
				else	this.OnMessage(Value);
				if(this.Identity)	setTimeout(this.GetFunction,100);
				}
			else	{
				if((this.VerifyTimeout*=2)>2000)	this.VerifyTimeout=2000;
				this.Verify();
				}
			}
		else	setTimeout(this.GetFunction,2000);
		}
	},
OnError:function(Identity)
	{
	if(Identity==this.Identity)	setTimeout(fastCSharp.ThisFunction(this,this.Check,[Identity]),2000);
	},
Check:function(Identity)
	{
	if(Identity==this.Identity)	setTimeout(this.GetFunction,8000);
	},
Verify:function()
	{
	fastCSharp.Ajax(this.OnVerifyFunction,this.VerifyPath,null,this.OnVerifyFunction);
	},
OnVerify:function(Value)
	{
	if(Value&&Value.isVerify)
		{
		this.VerifyInfo=Value.Return;
		if(this.Identity)	setTimeout(this.GetFunction,this.VerifyTimeout);
		}
	else if(this.Identity)	setTimeout(this.VerifyFunction,8000);
	}
		});
fastCSharp.Poll=new Poll();
})();


var Chat=	{
Load:function()
	{
	fastCSharp.$Id('User').Value(fastCSharp.Query.user);
	this.Poll=fastCSharp.NewObject('Poll',{VerifyPath:''});
	this.Poll.OnMessage.Add(fastCSharp.ThisFunction(this,this.OnPoll));
	this.Login();
	this.HtmlEditor=fastCSharp.NewObject('HtmlEditor',{ElementId:'MessageEditor',PasteLinkAjaxCallName:'pub.CrawlTitle',PasteImageAjaxCallName:'pub.PasteImage'});
	this.HtmlEditor.Start();
	fastCSharp.$Id('MessageEditor').Display(1);
	},
Login:function()
	{
	this.User=fastCSharp.$Value('User').Trim();
	if(this.User)
		{
		fastCSharp.PageView.SkinValue.NotLogin.Set(0);
		fastCSharp.PageView.SkinValue.LoginUser.Set('正在登录 '+this.User);
		fastCSharp.Ajax(fastCSharp.ThisFunction(this,this.OnLogin),'user.Login',{user:this.User});
		}
	},
OnLogin:function(Value)
	{
	if(Value.Return)
		{
		this.Poll.Query.UserVersion=Value.version;
		fastCSharp.PageView.SkinValue.LoginUser.Set(this.User);
		fastCSharp.PageView.SkinValue.IsLogin.Set(1);
		this.SetUsers(Value.Return);
		this.HtmlEditor.SetHtml('大家好，我是 '+this.User);
		this.Send();
		this.Poll.Start(this.User);
		}
	else	{
		alert('用户已注册 '+this.User);
		fastCSharp.PageView.SkinValue.LoginUser.Set('');
		fastCSharp.PageView.SkinValue.NotLogin.Set(1);
		}
	},
SetUsers:function(Users)
	{
	fastCSharp.PageView.SkinValue.Users.Set(Users.Remove(this.User));
	},
OnPoll:function(Value)
	{
	if(Value.Message)
		{
		if(Value.Message.Users)
			{
			this.Poll.Query.UserVersion=Value.Message.UserVersion;
			this.SetUsers(Value.Message.Users);
			}
		if(Value.Message.Messages)	this.AddMessage(Value.Message.Messages);
		}
	else	this.Poll.Stop();
	},
AddMessage:function(NewMessages)
	{
	var Messages=fastCSharp.PageView.SkinValue.Messages;
	Messages.Set((Messages.SkinValue||[]).concat(NewMessages));
	},
Logout:function()
	{
	fastCSharp.Ajax(null,'user.Logout',{user:this.User});
	this.Poll.Stop();
	this.User=null;
	fastCSharp.PageView.SkinValue.Users.Set([]);
	fastCSharp.PageView.SkinValue.Messages.Set([]);
	fastCSharp.PageView.SkinValue.LoginUser.Set('');
	fastCSharp.PageView.SkinValue.IsLogin.Set(0);
	fastCSharp.PageView.SkinValue.NotLogin.Set(1);
	},
Send:function()
	{
	var CheckUsers=fastCSharp.$Name('UserCheckbox').GetElements().Find(function(CheckBox){return CheckBox.checked;});
	var Value={user:this.User,message:this.HtmlEditor.GetHtml(),users:(CheckUsers.length?CheckUsers.ToArray(function(CheckBox){return CheckBox.value;}):null)};
	fastCSharp.Ajax(null,'user.Send',Value);
	this.AddMessage([{User:this.User,Time:new Date,Message:Value.message}]);
	this.HtmlEditor.SetHtml('');
	}
		};
fastCSharp.OnModule(['htmlEditor'],fastCSharp.ThisFunction(Chat,Chat.Load));